/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header$
 * $Id$
 * $Author$
 * $Date$
 * $Revision$
 */
package com.elancom.framework.test.testng;

import java.util.ArrayList;
import java.util.List;

import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IRowValueProvider;
import org.dbunit.dataset.ITable;
import org.dbunit.dataset.RowFilterTable;
import org.dbunit.dataset.filter.IRowFilter;

/**
 * @author elancom
 * @date 2012-7-20
 */
public class RowFilter implements IRowFilter {
	private String id;
	private List<Filter> filters = new ArrayList<>();
	private ITable table = null;

	/**
	 * @param id
	 */
	public RowFilter(String id) {
		super();
		this.id = id;
	}

	/**
	 * @param table
	 */
	public RowFilter(ITable table) {
		super();
		this.table = table;
	}

	/**
	 * @param table
	 * @param id
	 */
	public RowFilter(ITable table, String id) {
		super();
		this.table = table;
		this.id = id;
	}

	/**
	 * 
	 */
	public RowFilter() {
		super();
		// TODO Auto-generated constructor stub
	}

	public RowFilter filter(String columnName, String value) {
		Filter f = new Filter(columnName, value);
		filters.add(f);
		return this;
	}

	public ITable getTable() {
		if (table == null) {
			throw new RuntimeException("表不能为空");
		}

		RowFilterTable rowFilterTable = null;
		try {
			rowFilterTable = new RowFilterTable(table, this);
		} catch (DataSetException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}

		return rowFilterTable;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.dbunit.dataset.filter.IRowFilter#accept(org.dbunit.dataset.IRowValueProvider)
	 */
	@Override
	public boolean accept(IRowValueProvider rowValueProvider) {
		boolean pass = false;
		try {
			for (Filter f : filters) {
				Object obj = rowValueProvider.getColumnValue(f.getColumnName());
				if (obj != null) {
					if (obj instanceof String) {
						pass = obj.equals(f.getValue());
					} else {
						// FIXME
					}
				} else {
					pass = f.getValue() == null;
				}
			}
		} catch (DataSetException e) {
			e.printStackTrace();
		}
		return pass;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	class Filter {
		private String columnName;
		private Object value;

		/**
		 * @return the columnName
		 */
		public String getColumnName() {
			return columnName;
		}

		/**
		 * @param columnName
		 *            the columnName to set
		 */
		public void setColumnName(String columnName) {
			this.columnName = columnName;
		}

		/**
		 * @param columnName
		 * @param value
		 */
		public Filter(String columnName, Object value) {
			super();
			this.columnName = columnName;
			this.value = value;
		}

		/**
		 * 
		 */
		public Filter() {
			super();
			// TODO Auto-generated constructor stub
		}

		/**
		 * @return the value
		 */
		public Object getValue() {
			return value;
		}

		/**
		 * @param value
		 *            the value to set
		 */
		public void setValue(Object value) {
			this.value = value;
		}
	}

	/**
	 * @param table
	 *            the table to set
	 */
	public void setTable(ITable table) {
		this.table = table;
	}
}
